package com.netbao.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netbao.model.PageBean;
import com.netbao.model.PageResult;
import com.netbao.model.Result;
import com.netbao.pro.mapper.ProjectMapper;
import com.netbao.pro.model.Project;
import com.netbao.pro.model.ProjectExample;
import com.netbao.pro.service.ProjectService;
import com.netbao.sys.utils.ShiroUtils;
import com.netbao.veh.model.Vehicle;
import com.netbao.veh.vomapper.VehicleVoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private VehicleVoMapper vehicleVoMapper;
    @Override
    public PageResult list(PageBean pageBean, String cName) {
        ProjectExample projectExample=new ProjectExample();
        ProjectExample.Criteria criteria = projectExample.createCriteria().andIsdeleteEqualTo(false);
        if(ShiroUtils.getUserId().equals(1)){
            criteria.andEidEqualTo(ShiroUtils.getUserEntity().getEid().longValue());
        }
        if(!StringUtils.isEmpty(cName)){
            criteria.andCnameLike("%"+cName+"%");
        }
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        PageInfo<Project> projectPageInfo = new PageInfo<>(projectMapper.selectByExample(projectExample));
        return new PageResult(projectPageInfo.getTotal(),projectPageInfo.getList());
    }

    @Override
    public Result save(Project project) {
        project.setEid(ShiroUtils.getUserEntity().getEid().longValue());
        Date date = new Date();
        ProjectExample projectExample = new ProjectExample();
        ProjectExample.Criteria criteria = projectExample.createCriteria();
        if(project.getId()==null){
            project.setCcode("12346856");
            project.setOpid(ShiroUtils.getUserId().toString());
            project.setCreatetime(date);
            project.setCstatus((long) 1);
            projectMapper.insertSelective(project);
        }else {
            Project project1 = projectMapper.selectByPrimaryKey(project.getId());
            if(project.getCname()!=null){//编辑

            }
            if(project.getIscheck()!=null){//审核
                if(project.getIscheck()){
                    project.setCstatus((long)14);
                }else {
                    if(project1.getSdate().before(new Date())){
                        return Result.error("项目已启动无法驳回");
                    }
                    project.setCstatus((long)17);
                }
            }
            if(project.getIsdelete()!=null){//删除
                if(project1.getSdate().before(new Date())){
                    return Result.error("项目已启动，不可删除！");
                }
            }
            if(project.getIsstop()!=null){//停用
                if(!project1.getSdate().before(new Date())){
                    return Result.error("该项目还未启动！");
                }
                project.setCstatus((long)16);
            }
            projectMapper.updateByPrimaryKeySelective(project);
        }
        return Result.ok();
    }

    @Override
    public Result delete(Long id) {
        projectMapper.deleteByPrimaryKey(id);
        return Result.ok();
    }

    @Override
    public PageResult getvehicle(PageBean pageBean, Long proid, Integer eid) {
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        PageInfo<Vehicle> vehiclePageInfo = new PageInfo<>(vehicleVoMapper.getProVeh(proid,eid));
        return new PageResult(vehiclePageInfo.getTotal(),vehiclePageInfo.getList());
    }

    @Override
    @Transactional
    public Result binDingVeh(Long proId, String vehIds) {
        Project project = projectMapper.selectByPrimaryKey(proId);
        if(project.getSdate().before(new Date())){
            return Result.error("该项目已启动，可联系调度人更改车辆！");
        }
        vehicleVoMapper.clearPro(proId, ShiroUtils.getUserEntity().getEid());
        vehicleVoMapper.binDingVeh(proId,vehIds);
        return Result.ok();
    }
}
